// src/router/index.ts
import { createRouter, createWebHashHistory, RouteRecordRaw } from "vue-router";
import { useMyLoading } from "@/store/modules/myLoading";
import NProgress from "nprogress";
// import Layout from "@/components/Layout/Layout.vue";

export const Layout = () => import('@/components/Layout/Layout.vue');
export const LayoutRouterView = ()=> import('@/components/Layout/components/LayoutRouterView.vue');

// 静态路由
export const constantRoutes: RouteRecordRaw[] = [
  {
    path: "/",
    name: "layout",
    component: Layout,
    meta: { hidden: true },
    redirect: "/home",

    children: [
      {
        path: "home",
        name: "home",
        component: () => import("@/views/home/home.vue"),
        meta: { hidden: false, title: "首页", icon: "homepage", close: false },
      },
      {
        path: "test",
        name: "test",
        component: LayoutRouterView, //Layout, //() => import("@/views/test/test.vue"),
        meta: { hidden: false, title: "AI功能", icon: "api" },
        redirect: '/test/test1', // 默认重定向到 test1
        children: [
          {
            path: "test1",
            name: "test1",
            component: () => import("@/views/chat/Chat.vue"),
            meta: { hidden: false, title: "AI对话", icon: "chatDotSquare", close: true },
          },
          {
            path: "test2",
            name: "test2",
            component: () => import("@/views/test/test.vue"),
            meta: { hidden: false, title: "MJ绘画", icon: "picture", close: true },
          },
        ],
      },
    ],
  },
  {
    path: "/login",
    name: "login",
    component: () => import("@/views/login/login.vue"),
    meta: { hidden: true },
  },

  // {
  //   path: '/redirect',
  //   component: Layout,
  //   meta: { hidden: true },
  //   children: [
  //     {
  //       path: '/redirect/:path(.*)',
  //       component: () => import('@/views/redirect/index.vue')
  //     }
  //   ]
  // },

  // {
  //   path: '/login',
  //   component: () => import('@/views/login/index.vue'),
  //   meta: { hidden: true }
  // },

  // {
  //   path: '/',
  //   component: Layout,
  //   redirect: '/dashboard',
  //   children: [
  //     {
  //       path: 'dashboard',
  //       component: () => import('@/views/dashboard/index.vue'),
  //       name: 'Dashboard',
  //       meta: { title: 'dashboard', icon: 'homepage', affix: true }
  //     }
  //   ]
  // }
];

/**
 * 创建路由
 */
const router = createRouter({
  history: createWebHashHistory(),
  routes: constantRoutes as RouteRecordRaw[],
  // 刷新时，滚动条位置还原
  scrollBehavior: () => ({ left: 0, top: 0 }),
});

/**
 * 重置路由
 */
export function resetRouter() {
  router.replace({ path: "/login" });
  location.reload();
}

router.beforeEach((to, from, next) => {
  const myLoadingStore = useMyLoading();
  NProgress.start();
  const timId = setTimeout(() => {
    myLoadingStore.changeLoading(true);
  }, 1000);
  document.title = to.meta?.title ? String(to.meta.title) : 'AI';
  next();
  router.afterEach(() => {
    const myLoadingStore = useMyLoading();
    clearTimeout(timId);
    myLoadingStore.changeLoading(false);
    NProgress.done();
  });
});

export default router;
